package com.bosssoft.common.context;

public class UserBaseContextHolder extends BaseContextHolder {

    private static final String USER_ID_KEY = "USER_ID";
    private static final String USERNAME_KEY = "USERNAME";
    private static final String ROLES_KEY = "ROLES";

    private UserBaseContextHolder() {
        // Prevent instantiation
    }

    public static void setUser(UserInfo user) {
        if (user != null) {
            set(USER_ID_KEY, user.getUserId());
            set(USERNAME_KEY, user.getUsername());
            set(ROLES_KEY, user.getRoles());
        }
    }

    public static UserInfo getUser() {
        UserInfo user = new UserInfo();
        user.setUserId(get(USER_ID_KEY, Long.class));
        user.setUsername(get(USERNAME_KEY, String.class));
        user.setRoles(get(ROLES_KEY, String.class));
        return user;
    }

    public static void clearUser() {
        remove();
    }
}
